#include "Movable.h"
using namespace sf;

Movable::Movable(sf::Vector2f dimension, sf::Color color, sf::Vector2f velocity, sf::Vector2f position, AnimationHandlerFactory* animationHandlerFactory) :
		_velocity(velocity),
		_shape(RectangleShape(Vector2f(dimension.x,dimension.y))),
		_animationHandlerFactory(animationHandlerFactory)
{
	// set the properties of the Rectangle shape
	_shape.setPosition(position.x, position.y);
	//_shape.setOrigin(dimension.x/2 , dimension.y/2);
	
	_currentDirection=Right1;

	
	int dir=rand()%2;
	if(dir==1)
		_velocity.x=-_velocity.x;
}
Movable::~Movable()
{
}

sf::RectangleShape* Movable::getShape()
{
	return &_shape;
}

void Movable::move(float delta)
{
	_shape.move((int)_velocity.x*delta, (int)_velocity.y*delta);
	
	//Correct the coordinates of the object. Here it tests whether the object reached the left limit of the level 
	if (_shape.getGlobalBounds().left<0)
		_shape.move(-_shape.getGlobalBounds().left,0);
}

void Movable::setVelocity(Vector2f velocity)
{
	_velocity=velocity;
}

void Movable::addVelocity(Vector2f velocity)
{
	_velocity.x +=velocity.x;
	_velocity.y +=velocity.y;
}

Vector2f Movable::getVelocity() const
{
	return _velocity;
}

void Movable::slowDown()
{
	//horizontal speed
	if (_velocity.x>6)
		_velocity.x-=3;
	else if (_velocity.x<-6)
		_velocity.x+=3;
	else
		_velocity.x=0;
	
	//vertical speed
	if (_touching)
		_velocity.y=0;
	else if (_velocity.y <980)
		_velocity.y+=30;
	else if (_velocity.y >980)
		_velocity.y=980;
}